package com.da.landlord;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.espirit.weixin.core.WeixinUtil;
import com.googlecode.cswish.Config;
import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.struts.spring.SystemEnv;
import com.ra.landlord.Building;
import com.ra.landlord.BuildingService;

/**
 * 用于二维护下载和文档下载
 * @author PeterZ
 *
 */
@Service
public class DownloadService {

	@Resource
	private BuildingService buildingService;
	
	@Resource
	private WeixinUtil weixinUtil;
	
	@Resource
	private Config config;
	
	@Rest(type = "search")
	public void view(){
		//记录日志 dosomthing
		System.out.println("sdfsd");
		return;
	}
	
	/**
	 * 下载二维码图片
	 * @param model 楼层对象 id作为条件
	 * @param filename 楼层名
	 */
	@Rest(type = "search")
	public void downloadCodeImg(Building model,String filename){
		
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		if(model.getId()==null){
			return;
		}
		String[] paths = buildingService.printBuilding(model, 0, 0, 1);
		String urlStr = paths[0];
		
		try {
			//根据浏览器的不同处理汉字
			if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
				filename = URLEncoder.encode(filename, "UTF-8");
			} else {
				filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
			}
			response.setContentType("application/x-download; charset=utf-8");
			response.setHeader("Content-Disposition","attachment;filename="+filename+".jpg");
			
			URL url = new URL(urlStr);
			URLConnection urlConnection =  url.openConnection();
			
			InputStream in = urlConnection.getInputStream();
			OutputStream out = response.getOutputStream();
			
			byte[] byteArr = new byte[1024];
			int len;
			while((len=in.read(byteArr))>0){
				out.write(byteArr, 0, len);
			}
				
			in.close();
			downLoadFile();
		} catch (MalformedURLException e) {
				e.printStackTrace();
		} catch (IOException e) {
				e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 下载doc模板文件路经
	 * 此方法作废，可直接把路经写在页面当中
	 *http://localhost:8080/site/da/templet.doc
	 */
	public String downLoadFile(){
		boolean isEclipseEnv = "true".equals(System.getProperty(SystemEnv.RUNNING_IN_ECLIPSE));
		String siteUrl;
		
		if (isEclipseEnv) {
			siteUrl = "http://localhost:8080/";
		} else {
			siteUrl = weixinUtil.getWeixinConfig().getServerSiteName() + "/";
		}
		if(siteUrl.contains("null")){
			int index = siteUrl.indexOf("/");
			siteUrl = siteUrl.substring(index);
		}
		if(siteUrl.endsWith("/")){
			siteUrl+="/";
		}
		siteUrl+="site/da/bb.doc";
		return siteUrl;
	}
	
}
